﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICeFramework.DataManagement.Interfaces;
using Microsoft.SqlServer.Management.Smo;
using ICeFramework.DataManagement.Collections;
using ICeFramework.DataManagement.Enums;

namespace ICeFramework.DataManagement.MSSql
{
    public class MSSqlForeignKey : IForeignKey
    {
        #region public MSSqlForeignKey(ITable parent, ForeignKey foreignKey)
        /// <summary>
        /// Initializes a new instance of the <b>MSSqlForeignKey</b> class.
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="foreignKey"></param>
        public MSSqlForeignKey(ITable parent, ForeignKey foreignKey)
        {
            foreach (ForeignKeyColumn fkCol in foreignKey.Columns)
            {
                IForeignKeyColumn mssqlFK = new MSSqlForeignKeyColumn(this, fkCol);
                _columns.Add(mssqlFK);
            }
            _dateLastModified = foreignKey.DateLastModified;
            _deleteAction = GetSqlForeignKeyAction(foreignKey.DeleteAction);
            _id = foreignKey.ID;
            _isChecked = foreignKey.IsChecked;
            _isEnabled = foreignKey.IsEnabled;
            _isSystemNamed = foreignKey.IsSystemNamed;
            _name = foreignKey.Name;
            _notForReplication = foreignKey.NotForReplication;
            _parent = parent;
            _referencedKey = foreignKey.ReferencedKey;
            _referencedTable = foreignKey.ReferencedTable;
            _referencedTableSchema = foreignKey.ReferencedTableSchema;
            _scriptReferencedTable = foreignKey.ScriptReferencedTable;
            _scriptReferencedTableSchema = foreignKey.ScriptReferencedTableSchema;
            switch (foreignKey.State)
            {
                case SqlSmoState.Creating:
                    _state = SqlObjectState.Creating;
                    break;
                case SqlSmoState.Dropped:
                    _state = SqlObjectState.Dropped;
                    break;
                case SqlSmoState.Existing:
                    _state = SqlObjectState.Existing;
                    break;
                case SqlSmoState.Pending:
                    _state = SqlObjectState.Pending;
                    break;
                case SqlSmoState.ToBeDropped:
                    _state = SqlObjectState.ToBeDropped;
                    break;
            }
            _updateAction = GetSqlForeignKeyAction(foreignKey.UpdateAction);
        }
        #endregion

        #region public override string ToString()
        /// <summary>
        /// Returns a <see cref="string"/> that represents the current <see cref="ICeFramework.DataAccess.DataAccess.MSSql.MSSqlForeignKey"/>.
        /// </summary>
        /// <returns>A <see cref="string"/> that represents the current <see cref="ICeFramework.DataAccess.DataAccess.MSSql.MSSqlForeignKey"/>.</returns>
        public override string ToString()
        {
            return _name;
        }
        #endregion

        #region private SqlForeignKeyAction GetSqlForeignKeyAction(ForeignKeyAction action)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        private SqlForeignKeyAction GetSqlForeignKeyAction(ForeignKeyAction action)
        {
            SqlForeignKeyAction ret;
            switch (action)
            {
                case ForeignKeyAction.Cascade:
                    ret = SqlForeignKeyAction.Cascade;
                    break;
                case ForeignKeyAction.NoAction:
                    ret = SqlForeignKeyAction.NoAction;
                    break;
                case ForeignKeyAction.SetDefault:
                    ret = SqlForeignKeyAction.SetDefault;
                    break;
                case ForeignKeyAction.SetNull:
                    ret = SqlForeignKeyAction.SetNull;
                    break;
                default:
                    ret = SqlForeignKeyAction.NotAvailable;
                    break;
            }
            return ret;
        }
        #endregion

        private ForeignKeyColumnList _columns = new ForeignKeyColumnList();
        #region public ForeignKeyColumnList Columns
        /// <summary>
        /// Get/Sets the Columns of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public ForeignKeyColumnList Columns
        {
            get
            {
                return _columns;
            }
            set
            {
                _columns = value;
            }
        }
        #endregion

        private DateTime _dateLastModified;
        #region public DateTime DateLastModified
        /// <summary>
        /// Gets the DateLastModified of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public DateTime DateLastModified
        {
            get { return _dateLastModified; }
        }
        #endregion

        private SqlForeignKeyAction _deleteAction;
        #region public SqlForeignKeyAction DeleteAction
        /// <summary>
        /// Get/Sets the DeleteAction of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public SqlForeignKeyAction DeleteAction
        {
            get
            {
                return _deleteAction;
            }
            set
            {
                _deleteAction = value;
            }
        }
        #endregion

        private int _id;
        #region public int ID
        /// <summary>
        /// Gets the ID of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public int ID
        {
            get { return _id; }
        }
        #endregion

        private bool _isChecked;
        #region public bool IsChecked
        /// <summary>
        /// Get/Sets the IsChecked of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public bool IsChecked
        {
            get
            {
                return _isChecked;
            }
            set
            {
                _isChecked = value;
            }
        }
        #endregion

        private bool _isEnabled;
        #region public bool IsEnabled
        /// <summary>
        /// Get/Sets the IsEnabled of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public bool IsEnabled
        {
            get
            {
                return _isEnabled;
            }
            set
            {
                _isEnabled = value;
            }
        }
        #endregion

        private bool _isSystemNamed;
        #region public bool IsSystemNamed
        /// <summary>
        /// Gets the IsSystemNamed of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public bool IsSystemNamed
        {
            get { return _isSystemNamed; }
        }
        #endregion

        private string _name;
        #region public string Name
        /// <summary>
        /// Get/Sets the Name of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        #endregion

        private bool _notForReplication;
        #region public bool NotForReplication
        /// <summary>
        /// Gets the NotForReplication of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public bool NotForReplication
        {
            get { return _notForReplication; }
        }
        #endregion

        private ITable _parent;
        #region public ITable Parent
        /// <summary>
        /// Get/Sets the Parent of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public ITable Parent
        {
            get
            {
                return _parent;
            }
            set
            {
                _parent = value;
            }
        }
        #endregion

        private string _referencedKey;
        #region public string ReferencedKey
        /// <summary>
        /// Gets the ReferencedKey of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public string ReferencedKey
        {
            get { return _referencedKey; }
        }
        #endregion

        private string _referencedTable;
        #region public string ReferencedTable
        /// <summary>
        /// Get/Sets the ReferencedTable of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public string ReferencedTable
        {
            get
            {
                return _referencedTable;
            }
            set
            {
                _referencedTable = value;
            }
        }
        #endregion

        private string _referencedTableSchema;
        #region public string ReferencedTableSchema
        /// <summary>
        /// Get/Sets the ReferencedTableSchema of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public string ReferencedTableSchema
        {
            get
            {
                return _referencedTableSchema;
            }
            set
            {
                _referencedTableSchema = value;
            }
        }
        #endregion

        private string _scriptReferencedTable;
        #region public string ScriptReferencedTable
        /// <summary>
        /// Get/Sets the ScriptReferencedTable of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public string ScriptReferencedTable
        {
            get
            {
                return _scriptReferencedTable;
            }
            set
            {
                _scriptReferencedTable = value;
            }
        }
        #endregion

        private string _scriptReferencedTableSchema;
        #region public string ScriptReferencedTableSchema
        /// <summary>
        /// Get/Sets the ScriptReferencedTableSchema of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public string ScriptReferencedTableSchema
        {
            get
            {
                return _scriptReferencedTableSchema;
            }
            set
            {
                _scriptReferencedTableSchema = value;
            }
        }
        #endregion

        private SqlObjectState _state;
        #region public SqlObjectState State
        /// <summary>
        /// Gets the State of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public SqlObjectState State
        {
            get { return _state; }
        }
        #endregion

        private SqlForeignKeyAction _updateAction;
        #region public SqlForeignKeyAction UpdateAction
        /// <summary>
        /// Get/Sets the UpdateAction of the MSSqlForeignKey
        /// </summary>
        /// <value></value>
        public SqlForeignKeyAction UpdateAction
        {
            get
            {
                return _updateAction;
            }
            set
            {
                _updateAction = value;
            }
        }
        #endregion
    }
}
